package com.qyc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qyc.dto.AddOrUpdateFenceHouseDTO;
import com.qyc.enums.CodeMsgEnum;
import com.qyc.exception.SystemException;
import com.qyc.mapper.HurdlesMapper;
import com.qyc.pojo.FenceHouse;
import com.qyc.pojo.Hurdles;
import com.qyc.service.FenceHouseService;
import com.qyc.mapper.FenceHouseMapper;
import com.qyc.utils.BeanCopyUtils;
import com.qyc.utils.ResponseResult;
import com.qyc.vo.FenceHouseSelectByIdVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;
import java.util.Objects;

/**
* @author qinyc
* @description 针对表【manager_fence_house(栏舍信息表)】的数据库操作Service实现
* @createDate 2023-06-24 12:31:02
*/
@Service
public class FenceHouseServiceImpl extends ServiceImpl<FenceHouseMapper, FenceHouse>
    implements FenceHouseService{
    //注入栏舍Mapper层对象
    @Autowired
    private FenceHouseMapper fenceHouseMapper;

    //注入栏圈Mapper层对象
    @Autowired
    private HurdlesMapper hurdlesMapper;

    /**
     * @Author qinyc
     * @Description  分页多条件查询栏舍信息
     * @version: v1.0
     * @Date 16:13 2023/7/5
     **/
    @Override
    public ResponseResult queryPage(String fhName, Integer pageNum, Integer pageSize) {
        /**
         * @Author qinyc
         * @Description  mybatis-plus实现
         * @version: v1.0
         * @Date 16:10 2023/7/5
         **/
//        //创建条件选择器
//        LambdaQueryWrapper<FenceHouse> fenceHouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        //如果存在栏舍名称参数，那么按照栏舍名称进行模糊匹配
//        fenceHouseLambdaQueryWrapper.like(StringUtils.hasText(fhName),FenceHouse::getFhName,fhName);
//        //开启PageHelper插件
//        PageHelper.startPage(pageNum,pageSize,true);
//        //核心查询语句
//        List<FenceHouse> fenceHouses = fenceHouseMapper.selectList(fenceHouseLambdaQueryWrapper);
//        //将查询的信息封装在PageInfo对象中
//        PageInfo pageInfo = new PageInfo(fenceHouses);
//        //返回统一响应对象
//        return ResponseResult.success(pageInfo);
        
        /**
         * @Author qinyc
         * @Description  mybatis实现
         * @version: v1.0
         * @Date 16:10 2023/7/5
         **/
        //开启PageHelper插件
        PageHelper.startPage(pageNum,pageSize,true);
        //核心查询语句
        List<FenceHouse> fenceHouses = fenceHouseMapper.queryPage(fhName);
        //将查询的信息封装在PageInfo对象中
        PageInfo pageInfo = new PageInfo(fenceHouses);
        //返回统一响应对象
        return ResponseResult.success(pageInfo);
    }

    /**
     * @Author qinyc
     * @Description  根据栏舍编号查询栏舍信息以及栏圈信息
     * @version: v1.0
     * @Date 13:27 2023/6/24
     **/
    @Override
    public ResponseResult selectById(String fhId) {
        /**
         * @Author qinyc
         * @Description  mybatis-plus实现
         * @version: v1.0
         * @Date 16:44 2023/7/5
         **/
//        //根据栏舍ID查询栏舍信息
//        FenceHouse fenceHouse = fenceHouseMapper.selectById(fhId);
//        //创建条件选择器
//        LambdaQueryWrapper<Hurdles> hurdlesLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        //根据栏舍ID匹配栏圈信息（精确匹配）
//        hurdlesLambdaQueryWrapper.eq(Hurdles::gethFenceId,fhId);
//        //查询符合条件的栏圈信息集合
//        List<Hurdles> hurdles = hurdlesMapper.selectList(hurdlesLambdaQueryWrapper);
//        //bean拷贝
//        FenceHouseSelectByIdVO fenceHouseSelectByIdVO = BeanCopyUtils.copyBean(fenceHouse, FenceHouseSelectByIdVO.class);
//        //将栏圈集合信息封装在vo对象中
//        fenceHouseSelectByIdVO.setMhs(hurdles);
//        //返回统一响应
//        return ResponseResult.success(fenceHouseSelectByIdVO);

        /**
         * @Author qinyc
         * @Description  mybatis实现
         * @version: v1.0
         * @Date 16:44 2023/7/5
         **/
        //调用mapper层方法,查询栏舍信息包含对应栏圈信息
        FenceHouseSelectByIdVO fenceHouseSelectByIdVO = fenceHouseMapper.selectHurdlesById(fhId);
        //返回成功响应数据
        return ResponseResult.success(fenceHouseSelectByIdVO);

    }


    /**
     * @Author qinyc
     * @Description  新增或修改栏舍
     * @version: v1.0
     * @Date 17:23 2023/6/24
     **/
    @Override
    public ResponseResult saveOrUpdate(AddOrUpdateFenceHouseDTO addOrUpdateFenceHouseDTO) {
        //bean拷贝
        FenceHouse fenceHouse = BeanCopyUtils.copyBean(addOrUpdateFenceHouseDTO, FenceHouse.class);
        /*
         * 根据请求体中是否包含栏舍ID来判断
         */
        if (Objects.isNull(addOrUpdateFenceHouseDTO.getFhId())){
            /*
             * 请求体中不含有栏舍ID  新增操作
             */
            fenceHouseMapper.insert(fenceHouse);
            return ResponseResult.success();
        }else{
            /*
             * 请求体中含有栏舍ID 修改操作
             */
            fenceHouseMapper.updateById(fenceHouse);
            return ResponseResult.success();
        }
    }

    /**
     * @Author qinyc
     * @Description  根据id删除栏舍信息
     * @version: v1.0
     * @Date 10:30 2023/6/25
     **/
    @Override
    public ResponseResult deleteById(String hId) {
        //判断ID是否为空
        if (!StringUtils.hasText(hId)){
            throw new SystemException(CodeMsgEnum.FENCE_HOUSE_ID_IS_NOT_NULL);
        }
        //根据栏舍ID查询栏舍信息
        FenceHouse fenceHouse = fenceHouseMapper.selectById(hId);
        //判断查询到的栏舍ID是否存在
        if (Objects.isNull(fenceHouse)){
            //栏舍对象为空
            throw new SystemException(CodeMsgEnum.FENCE_HOUSE_IS_NOT_EXIST);
        }
        //参数校验完毕，执行删除操作
        fenceHouseMapper.deleteById(hId);
        //返回成功响应
        return ResponseResult.success();
    }

    /**
     * @Author qinyc
     * @Description  批量删除栏舍
     * @version: v1.0
     * @Date 10:51 2023/6/25
     **/
    @Override
    public ResponseResult deleteByIdAll(List<String> hIds) {
        /*
         * 参数校验
         */
        //参数校验完毕
        fenceHouseMapper.deleteBatchIds(hIds);
        //返回成功响应
        return ResponseResult.success();
    }


}




